package edu.hrbu.controller;

import com.google.gson.Gson;
import edu.hrbu.constant.MyConst;
import edu.hrbu.pojo.Menu;
import edu.hrbu.pojo.Role;
import edu.hrbu.pojo.User;
import edu.hrbu.service.MenuService;
import edu.hrbu.service.RoleService;
import edu.hrbu.service.UserService;
import edu.hrbu.service.impl.MenuServiceImpl;
import edu.hrbu.service.impl.RoleServiceImpl;
import edu.hrbu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 徐登宇
 * @date 2022-10-30 10:30
 */
@WebServlet("/role")
public class RoleController extends HttpServlet {
    MenuService menuService = new MenuServiceImpl();
    UserService userService = new UserServiceImpl();
    RoleService roleService = new RoleServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String type = request.getParameter("type");
        if ("roleAnaly".equals(type)) {
            doRoleAnaly(request, response);
        } else if ("roleList".equals(type)) {
            doRoleList(request, response);
        } else if ("detail".equals(type)) {
            doDetail(request, response);
        } else if ("menuDetail".equals(type)) {
            doMenuDetail(request, response);
        } else if("roleInfo".equals(type)) {
            doRoleInfo(request, response);
        }
    }

    private void doRoleInfo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Role> roleList = roleService.getRoleList();
        request.setAttribute("roleList", roleList);
        request.getRequestDispatcher(request.getContextPath() + "/role_parent.jsp").forward(request, response);
    }

    private void doMenuDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        List<Menu> menuList = menuService.getAllMenuByRoleId(roleId);
        List<User> userList = userService.getUserListByRoleId(roleId);
        request.setAttribute("userList", userList);
        request.setAttribute("menuList", menuList);
        request.getRequestDispatcher(request.getContextPath() + "/role_menu_detail.jsp").forward(request, response);
    }

    private void doDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        List<User> userList = userService.getUserListByRoleId(roleId);
//        List<Role> roleList = roleService.getRoleList();
        request.setAttribute("userList", userList);
//        request.setAttribute("roleList", roleList);
        request.getRequestDispatcher(request.getContextPath() + "/role_detail.jsp").forward(request, response);
    }

    private void doRoleList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Role> roleList = roleService.getRoleAndCount();
        List<Menu> roleMenuList = menuService.getMenuList(MyConst.ROOT_ID, MyConst.ROOT_MENU_ID);
        List<Menu> empMenuList = menuService.getMenuList(MyConst.EMP_ID, MyConst.ROOT_MENU_ID);
        List<Menu> userMenuList = menuService.getMenuList(MyConst.USER_ID, MyConst.ROOT_MENU_ID);
//        Gson gson = new Gson();
//        String roleListJson = gson.toJson(roleList);
        request.setAttribute("roleList", roleList);
        request.setAttribute("roleMenuList", roleMenuList);
        request.setAttribute("empMenuList", empMenuList);
        request.setAttribute("userMenuList", userMenuList);
        request.getRequestDispatcher(request.getContextPath()+"/role_list.jsp").forward(request, response);
    }

    private void doRoleAnaly(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取权限列表
        List<Role> roleList = roleService.getRoleAndCount();
        Gson gson = new Gson();
        String roleListJson = gson.toJson(roleList);
        request.setAttribute("roleListJson", roleListJson);
        request.getRequestDispatcher(request.getContextPath() + "/role_analyse.jsp").forward(request, response);
    }
}
